package cn.manyufun.myf.ui.common.model;

import org.json.JSONException;
import org.json.JSONObject;

import cn.manyufun.myf.api.Api;
import cn.manyufun.myf.api.HostType;
import cn.manyufun.myf.app.AppConstant;
import cn.manyufun.myf.bean.ArticleListBean;
import cn.manyufun.myf.bean.FollowListBean;
import cn.manyufun.myf.bean.ReplyListBean;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.commonutils.APIUtils;
import cn.manyufun.myf.common.commonutils.DisplayUtil;
import cn.manyufun.myf.ui.common.contact.CommonListContract;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;

import static cn.manyufun.myf.app.AppConstant.TYPE_HOT;

/**
 * @author Kevin Xiao
 * @date Create Date: 2018-03-01
 * @Description
 */
public class CommonListModel implements CommonListContract.Model {
    @Override
    public Observable<FollowListBean> getCommonListData(int pageIndex, int pageSize, int type) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ImageSize", DisplayUtil.dip2px(91f) + "*" + DisplayUtil.dip2px(127f));
            root.put("PageIndex", pageIndex);
            root.put("PageSize", pageSize);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        if (type == AppConstant.TYPE_RECOMMEND) {
            return Api.getDefault(HostType.manyufun).getNewsRecommendForYou(requestBody)
                    .compose(RxSchedulers.<FollowListBean>io_main());
        } else if (type == TYPE_HOT) {
            return Api.getDefault(HostType.manyufun).getHotList(requestBody)
                    .compose(RxSchedulers.<FollowListBean>io_main());
        } else {
            return Api.getDefault(HostType.manyufun).getRankList(requestBody)
                    .compose(RxSchedulers.<FollowListBean>io_main());
        }
    }

    @Override
    public Observable<ReplyListBean> getComicsCommentListData(int pageIndex, int pageSize, long comicsID) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ComicId", comicsID);
            root.put("PageIndex", pageIndex);
            root.put("PageSize", pageSize);
            root.put("Desc", true);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());

        return Api.getDefault(HostType.manyufun).getComicsReplyList(requestBody)
                .compose(RxSchedulers.<ReplyListBean>io_main());
    }

    @Override
    public Observable<ReplyListBean> getArticleCommentListData(int pageIndex, int pageSize, long articleID) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ArticleId", articleID);
            root.put("PageIndex", pageIndex);
            root.put("PageSize", pageSize);
            root.put("Desc", true);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());

        return Api.getDefault(HostType.manyufun).getArticleReplyList(requestBody)
                .compose(RxSchedulers.<ReplyListBean>io_main());
    }

    @Override
    public Observable<ArticleListBean> getComicArticleListData(int pageIndex, int pageSize, long comicsID) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ComicId", comicsID);
            root.put("PageIndex", pageIndex);
            root.put("PageSize", pageSize);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());

        return Api.getDefault(HostType.manyufun).getArticleListByComics(requestBody)
                .compose(RxSchedulers.<ArticleListBean>io_main());

    }
}
